package cn.edu.bit.lixin.blog.action;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import cn.edu.bit.lixin.blog.biz.BlogManager;
import cn.edu.bit.lixin.blog.biz.ReplyManager;
import cn.edu.bit.lixin.blog.entity.Blog;
import cn.edu.bit.lixin.blog.entity.Reply;
import cn.edu.bit.lixin.blog.util.VerifyCodeUtil;

public class ReplyAction extends BaseActionSupport {
	private ReplyManager replyManager;
	private BlogManager blogManager;
	
	
	//action注入的参数
	private int blogid;
	private String replierName;
	private String replierEmail;
	private String verifyCode;
	public String getVerifyCode() {
		return verifyCode;
	}

	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}

	private String content;


	public int getBlogid() {
		return blogid;
	}

	public void setBlogid(int blogid) {
		this.blogid = blogid;
	}

	public String getReplierName() {
		return replierName;
	}

	public void setReplierName(String replierName) {
		this.replierName = replierName;
	}

	public String getReplierEmail() {
		return replierEmail;
	}

	public void setReplierEmail(String replierEmail) {
		this.replierEmail = replierEmail;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public BlogManager getBlogManager() {
		return blogManager;
	}

	public void setBlogManager(BlogManager blogManager) {
		this.blogManager = blogManager;
	}

	public ReplyManager getReplyManager() {
		return replyManager;
	}

	public void setReplyManager(ReplyManager replyManager) {
		this.replyManager = replyManager;
	}
	
	/**
	 * 首先调用ajaxCreateReply创建一个Reply对象
	 * 然后将这个Reply对象存入数据库中
	 * @return
	 */
	public void ajaxCommitReply(){
		
		HttpServletResponse response = ServletActionContext.getResponse();//取response对象
		HttpServletRequest request = ServletActionContext.getRequest();//取request对象
		
		//服务器响应
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Cache-Control", "no-cache");

		//首先判断验证码是否正确
		if (!replyManager.judgeVerifyCode(verifyCode, (String)this.getSession("verifyCode"))){
			try {
				response.getWriter().print("1"); //1代表验证码不正确
				response.getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return;
		}
		
		Reply reply = ajaxCreateReply();//创建一个reply对象
		StringBuffer stringBuffer = new StringBuffer();//返回给ajax回调函数的信息暂时写在这里
		if (replyManager.saveReply(reply)){
			System.out.println(reply.getReplierName() + "   " + reply.getPubTime());
			
			//调整显示的时间格式
			 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			 String dateString = formatter.format(reply.getPubTime());
			
			//构造返回信息的html
			stringBuffer.append("<DIV class=\"comment-entry-alt\">");
			stringBuffer.append("<DIV class=\"comment-entry-meta\"<CITE>  ");
			stringBuffer.append(dateString + "," + reply.getReplierName() + "发表了:</CITE>");
			stringBuffer.append("</DIV><DIV class=\"comment-entry-content\"<P>");
			stringBuffer.append(reply.getContent() + "</P></DIV></DIV>");
			
			try {
				response.getWriter().print(stringBuffer.toString());
				response.getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			System.out.println(stringBuffer.toString());
		}
	}
	
	/**
	 * 根据js传递过来的参数构造一个新的评论对象
	 * 由ajaxCommitReply调用
	 * @return
	 */
	private Reply ajaxCreateReply(){
		Reply reply = new Reply();
		reply.setProperty(0);
		Date date = new Date();
		reply.setPubTime(date);
		reply.setReplierName(replierName);
		reply.setReplierId(-1);
		reply.setContent(content);
		reply.setReplierEmail(replierEmail);
		
		Blog blog = blogManager.getSpecificBlog(blogid);
		reply.setBlog(blog);
		
		return reply;
	}
	
	/**
	 * 生成验证码
	 */
	public void generateVerifyImage(){
		HttpServletResponse response = ServletActionContext.getResponse();
		
		String verifyCode = VerifyCodeUtil.generateVerifyCode();//生成验证码中的字符
		this.setSession("verifyCode", verifyCode);
		BufferedImage image = VerifyCodeUtil.generateVerifyImage(verifyCode);//将生成的字符传入，生成一个验证码图片
		try {
			ImageIO.write(image, "JPG", response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public String deleteReply(){
		int replyid = this.getParameterInt("reply");
		
		if (replyManager.deleteReply(replyid)){
			return SUCCESS;
		}
		
		return INPUT;
	}
}
